﻿package dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import dao.BookDao;
import model.Book;
import util.JDBCUtiles;

public class BookDaoImpl implements BookDao {

	@Override
	public List<Book> getAllBook() throws SQLException {
		// 得到QueryRunner对象
		QueryRunner qr = JDBCUtiles.getQueryRunner();
		// sql语句
		String sql = "select * from books";
		return qr.query(sql, new BeanListHandler<Book>(Book.class));
	}


	@Override
	public boolean addBook(Book book) throws SQLException {
		// 得到QueryRunner对象
		QueryRunner qr = JDBCUtiles.getQueryRunner();
		// sql语句
		String sql = "insert into books (BookIsbn,BookName,BookAuthor,BookPurchase,BookPrice,bookPublishingTime,BookNum)"
				+ " values(?,?,?,?,?,?,?)";
		int i = qr.update(sql, book.getBookIsbn(), book.getBookName(), book.getBookAuthor(), book.getBookPurchase(),
				book.getBookPrice(), book.getBookPublishingTime(), book.getBookNum());
		return i > 0 ? true : false;
	}

	@Override
	public boolean deletebook(Book book) throws SQLException {
		// 得到QueryRunner对象
		QueryRunner qr = JDBCUtiles.getQueryRunner();
		// sql语句
		String sql = "delete from books where BookIsbn = ?";
		int i = qr.update(sql, book.getBookIsbn());
		return i > 0 ? true : false;
	}

}
